package com.louis.iread.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.louis.iread.R;
import com.louis.iread.adapter.NovelOpAdapter;
import com.louis.iread.adapter.NovelOpOption;
import com.louis.iread.bean.Config;
import com.louis.iread.event.EventType;
import com.louis.iread.event.ReadEvent;
import com.louis.iread.skin.SkinMgr;
import com.louis.iread.utils.GlideCacheUtil;
import com.yarolegovich.lovelydialog.LovelyChoiceDialog;
import com.yarolegovich.lovelydialog.LovelyDialogCompat;
import com.yarolegovich.lovelydialog.LovelyStandardDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;


public class SettingsFrm extends Fragment {
    private static final String ARG_TAG = "settings";
    private SwitchCompat mUpdateOnStartSwitch;
    private TextView mCacheSizeView;
    private TextView mSortView;
    private TextView mPageView;
    public SettingsFrm() {
        // Required empty public constructor
    }

    public static SettingsFrm newInstance(String tag) {
        SettingsFrm fragment = new SettingsFrm();
        Bundle args = new Bundle();
        args.putString(ARG_TAG, tag);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onHiddenChanged (boolean hidden) {
        if (!hidden) {
            if (mCacheSizeView != null) {
                mCacheSizeView.setText(GlideCacheUtil.getInstance().getCacheSize());
            }
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        EventBus.getDefault().register(this);
        View contentView = inflater.inflate(R.layout.fragment_settings, container, false);
        mUpdateOnStartSwitch = (SwitchCompat) contentView.findViewById(R.id.update_on_start);
        boolean isUpdateOnStart = Config.getUpdateOnStart();
        mUpdateOnStartSwitch.setChecked(isUpdateOnStart);
        mUpdateOnStartSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Config.setUpdateOnStart(isChecked);
            }
        });

        mCacheSizeView = (TextView) contentView.findViewById(R.id.tvCacheSize);
        mCacheSizeView.setText(GlideCacheUtil.getInstance().getCacheSize());

        RelativeLayout clearCache = (RelativeLayout) contentView.findViewById(R.id.cleanCache);
        clearCache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new LovelyStandardDialog(getActivity())
                        .setButtonsColorRes(R.color.darkDeepOrange)
                        .setIcon(R.drawable.ic_star_border_white_36dp)
                        .setTitle(R.string.delete_ask)
                        .setMessage(R.string.clear_img_cache)
                        .setPositiveButton(android.R.string.ok, LovelyDialogCompat.wrap(new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                GlideCacheUtil.getInstance().cleanCacheDisk();
                                mCacheSizeView.setText(GlideCacheUtil.getInstance().getCacheSize());
                            }
                        }))
                        .setNegativeButton(android.R.string.no, null)
                        .show();
            }
        });
        RelativeLayout about = (RelativeLayout) contentView.findViewById(R.id.about);
        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), AboutActivity.class);
                v.getContext().startActivity(intent);
            }
        });
        mSortView = (TextView) contentView.findViewById(R.id.mTvSort);
        if (Config.SORT_TYPE_READ.equals(Config.getSortType())) {
            mSortView.setText(R.string.sort_read_time);
        } else {
            mSortView.setText(R.string.sort_update_time);
        }
        RelativeLayout sort = (RelativeLayout) contentView.findViewById(R.id.bookshelfSort);
        sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<NovelOpOption> opOptionList = new ArrayList<NovelOpOption>();
                opOptionList.add(new NovelOpOption(R.string.sort_read_time));
                opOptionList.add(new NovelOpOption(R.string.sort_update_time));
                ArrayAdapter<NovelOpOption> novelOpOptionArrayAdapter = new NovelOpAdapter(getActivity(), opOptionList);
                new LovelyChoiceDialog(getActivity())
                        .setTitle(R.string.book_sort)
                        .setIcon(R.drawable.ic_star_border_white_36dp)
                        .setItems( novelOpOptionArrayAdapter, new LovelyChoiceDialog.OnItemSelectedListener<NovelOpOption>() {
                            @Override
                            public void onItemSelected(int position, NovelOpOption item) {
                                if (item.strId == R.string.sort_read_time) {
                                    Config.setSortType(Config.SORT_TYPE_READ);
                                    mSortView.setText(R.string.sort_read_time);
                                } else if (item.strId == R.string.sort_update_time) {
                                    Config.setSortType(Config.SORT_TYPE_UPDATE);
                                    mSortView.setText(R.string.sort_update_time);
                                }
                                EventBus.getDefault().post(new ReadEvent(EventType.UPDATE_BOOK_LIST));
                            }
                        })
                        .show();
            }
        });
        mPageView = (TextView) contentView.findViewById(R.id.tvFlipStyle);
        if (Config.PAGE_SLIDE.equals(Config.getPageMethod())) {
            mPageView.setText(R.string.page_slide);
        } else {
            mPageView.setText(R.string.page_none);
        }
        RelativeLayout page = (RelativeLayout) contentView.findViewById(R.id.rlFlipStyle);
        page.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<NovelOpOption> opOptionList = new ArrayList<NovelOpOption>();
                opOptionList.add(new NovelOpOption(R.string.page_none));
                opOptionList.add(new NovelOpOption(R.string.page_slide));
                ArrayAdapter<NovelOpOption> novelOpOptionArrayAdapter = new NovelOpAdapter(getActivity(), opOptionList);
                new LovelyChoiceDialog(getActivity())
                        .setTitle(R.string.page_method)
                        .setIcon(R.drawable.ic_star_border_white_36dp)
                        .setItems( novelOpOptionArrayAdapter, new LovelyChoiceDialog.OnItemSelectedListener<NovelOpOption>() {
                            @Override
                            public void onItemSelected(int position, NovelOpOption item) {
                                if (item.strId == R.string.page_none) {
                                    Config.setPageMethod(Config.PAGE_NONE);
                                    mPageView.setText(R.string.page_none);
                                } else if (item.strId == R.string.page_slide) {
                                    Config.setPageMethod(Config.PAGE_SLIDE);
                                    mPageView.setText(R.string.page_slide);
                                }
                                EventBus.getDefault().post(new ReadEvent(EventType.UPDATE_BOOK_LIST));
                            }
                        })
                        .show();
            }
        });

        return contentView;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(ReadEvent event) {
        switch (event.getEventType()) {

            default:
                break;
        }
    }

    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            return;
        }
        SkinMgr.getInstance().applyView(view);
    }

}
